package com.zy.eureka.client.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("eureka")
public class EurekaTestController {

    @Resource
    private DiscoveryClient discoveryClient;

    @RequestMapping("serviceList")
    public String getServiceList(String serviceId) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        List<String> urls = instances.stream().map(ServiceInstance::getUri).map(URI::toASCIIString).collect(Collectors.toList());
        return urls.toString();
    }
}
